﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Windows.Forms;
using System.Drawing;

namespace LabelPrintSystem
{
    public static class ClsDifinition
    {
        public const int RUNNING_MODE = 0;//0-->天线部门，others-->射频部门 
        public const bool isDebug = false;

        public const string keys1ForMOD = "b1";
        public const string keys2ForMOD = "b2";
        public static string key1ForANTENNA = "条码";
        public static string key2ForANTENNA = "二维码";
    }

    public class ClsEventMode : EventArgs
    {
        public enum en_evtMode : int
        {
            evt_log = 0,
            evt_err = 1,
            evt_get_temple = 2,
            evt_complete = 3,
            evt_update_lb = 4,
            evt_Tips = 5
        }
        public en_evtMode evt_mode { set; get; }
        public ClsEventMode(en_evtMode evt_mode = en_evtMode.evt_log)
        {
            this.evt_mode = evt_mode;
        }
    }
    public enum TempleType : int
    {
        procode = 0,
        model = 1,
        manual = 2
    }
    public class Function
    {
        public static string GetIPAddress()
        {//PC-IP
            var host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (var ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    return ip.ToString();
                }
            }
            return "";
        }
        public static string GetCpuID()
        {//CPU-ID
            try
            {
                string cpuInfo = "";
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                }
                moc = null;
                mc = null;
                return cpuInfo;
            }
            catch
            {
                return "unknow";
            }
        }
        public static void WriteException(Exception ex)
        {//保存软件异常闪退信息
            try
            {
                FileStream fs = new FileStream(Application.StartupPath + "\\Error.txt",
                               FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss    ") + ex.Message);
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss    ") + ex.StackTrace);
                sw.Close();
                fs.Close();
            }
            catch (Exception) { }
        }

        public static bool IsDigitOrLetter(string strMessage, int iMinLong = 0, int iMaxLong = 100)
        {//正则表达式，校验字符串是否为纯数字字母组成
            bool bResult = false;

            //开头匹配一个字母或数字+匹配两个十进制数字+匹配一个字母或数字+匹配两个相同格式的的（-加数字）+已字母或数字结尾  
            //如：1111-111-1119  
            //string pattern = @"^[a-zA-Z0-9]\d{2}[a-zA-Z0-9](-\d{3}){2}[A-Za-z0-9]$";  

            //string pattern = @"^[a-zA-Z0-9]\d{2}$"; //开头以字母或数字，然后后面加两个数字字符  

            string pattern = @"^[a-zA-Z0-9]*$"; //匹配所有字符都在字母和数字之间  

            //string pattern = @"^[a-z0-9]*$"; //匹配所有字符都在小写字母和数字之间  

            //string pattern = @"^[A-Z][0-9]*$"; //以大写字母开头，后面的都是数字  

            //string pattern = @"^\d{3}-\d{2}$";//匹配 333-22 格式,三个数字加-加两个数字  

            if (strMessage.Length >= iMinLong && strMessage.Length <= iMaxLong)//判断字符串长度  
            {
                if (System.Text.RegularExpressions.Regex.IsMatch(strMessage, pattern))
                {
                    bResult = true;
                }
                else
                {
                    bResult = false;
                }
            }
            return bResult;
        }

        public static bool IsMatchLength(string strMessage,int iMinLong = 0, int iMaxLong = 100)
        {
            if (strMessage.Length > iMinLong && strMessage.Length < iMaxLong)
            {
                return true;
            }
            return false;
        }

        public static void CbAutoFix(object sender, EventArgs e)
        {//ComboBox下拉框自动填充
            ComboBox senderComboBox = (ComboBox)sender;
            int width = senderComboBox.DropDownWidth;
            Graphics g = senderComboBox.CreateGraphics();
            Font font = senderComboBox.Font;
            int vertScrollBarWidth =
                (senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
                ? SystemInformation.VerticalScrollBarWidth : 0;

            int newWidth;

            foreach (string s in (((ComboBox)sender).Items))
            {
                newWidth = (int)g.MeasureString(s, font).Width
                    + vertScrollBarWidth;
                if (width < newWidth)
                {
                    width = newWidth;
                }
            }
            senderComboBox.DropDownWidth = width;
        }
    }
}
